﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// Word控件
    /// 用于Word文件的查看及修改操作
    /// </summary>
    public partial class WordControl : PMFDataControl
    {
        #region Properties

        public int TableId { get; set; }
        public string TableName { get; set; }
        public string UserName { get; set; }
        public int ViewId { get; set; }
        public int RecordId { get; set; }

        #endregion

        #region Constructors

        public WordControl(int tableId, string tableName, string userName, int viewId)
        {
            InitializeComponent();

            this.TableId = tableId;
            this.TableName = tableName;
            this.UserName = userName;
            this.ViewId = viewId;
        }

        #endregion

        #region DependencyProperty

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(WordControl), new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        /// <summary>
        /// Word文件名称
        /// </summary>
        public string WordSource
        {
            get { return (string)GetValue(WordSourceProperty); }
            set { SetValue(WordSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WordSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WordSourceProperty =
            DependencyProperty.Register("WordSource", typeof(string), typeof(WordControl), new PropertyMetadata("default(string)", OnWordSourcePropertyChangedCallback));

        #endregion

        #region Override Control Methods

        public override void UpdateSource()
        {
            this.GetBindingExpression(WordControl.WordSourceProperty).UpdateSource();
        }

        #endregion

        #region Property Callback

        /// <summary>
        /// ReadOnly属性变化时进行的操作
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WordControl control = d as WordControl;
            control.SetReadOnly((bool)e.NewValue);
        }

        /// <summary>
        /// WordSource属性变化时进行的操作
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        static void OnWordSourcePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WordControl control = d as WordControl;
            DataInstance detail = control.DataContext as DataInstance;

            if (detail == null)
            {
                control.RecordId = 0;
            }
            else
            {
                //control.RecordId = detail.Id;
            }

            if (detail.Id != Guid.Empty)
            {
                // 还没有保存
                control.m_hyperlinkButtonWord.Content = "保存之后，才可以使用word模板!";
                control.m_hyperlinkButtonWord.IsEnabled = false;
            }
            else
            {
                // 如果数据中还有必填项没有填，那就不能使用模板
                bool isFixed = true;

                //for (int index = 0; index < detail.Fields.Count; index++)
                //{
                //    if (detail.Fields[index].IsVisible && !detail.Fields[index].IsReadOnly
                //        && detail.Fields[index].Type != "word"
                //        && (!detail.Fields[index].IsSaveNullable)
                //        && string.IsNullOrEmpty(detail.Fields[index].Value))
                //    {
                //        isFixed = false;
                //        break;
                //    }
                //}

                if (!isFixed)
                {
                    // 保存必填项还没有填完
                    control.m_hyperlinkButtonWord.Content = "请将必填项填完之后，才可以使用word模板!";
                    control.m_hyperlinkButtonWord.IsEnabled = false;
                }
                else
                {
                    string source = (string)e.NewValue;
                    if (string.IsNullOrEmpty(source))
                    {
                        control.m_hyperlinkButtonWord.Content = "使用模板";
                    }
                    else
                    {
                        control.m_hyperlinkButtonWord.Content = "正文.doc";
                    }
                    control.m_hyperlinkButtonWord.IsEnabled = true;
                }
            }
        }

        #endregion

        #region Border Event Handlers

        /// <summary>
        /// Border的按下鼠标操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBorderMouseEnter(object sender, MouseEventArgs e)
        {
            _ctlMessage.Visibility = System.Windows.Visibility.Collapsed;
        }

        #endregion

        #region HyperlinkButton Events

        /// <summary>
        /// 打开Word文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnHyperlinkButtonWordClick(object sender, RoutedEventArgs e)
        {
            if (IsReadOnly)
            {
                WordWindow.OpenReadWord(UserName, this.TableId, TableName, ViewId, RecordId);
            }
            else
            {
                WordWindow wordWindow = new WordWindow();
                wordWindow.Close += new Action<bool>(OnWordWindowClosed);
                wordWindow.OpenWord(UserName, this.TableId, this.TableName, this.ViewId, this.RecordId);
            }
        }

        #endregion

        #region User Defined Methods

        /// <summary>
        /// 通过传入值设置读写Word文件操作
        /// </summary>
        /// <param name="isReadOnly"></param>
        private void SetReadOnly(bool isReadOnly)
        {
        }

        /// <summary>
        /// 弹出的Word操作窗口关闭后执行的操作
        /// </summary>
        /// <param name="obj"></param>
        void OnWordWindowClosed(bool obj)
        {
            if (obj)
            {
                DataInstance detail = this.DataContext as DataInstance;
                WordSource = string.Format("{0}.doc", detail.Id);
            }
        }

        #endregion

    }
}
